package main

import (
	"fmt"
	"math"
)

type (
	//十字链路
	GameNode struct {
		xPrev *GameNode
		xNext *GameNode
		yPrev *GameNode
		yNext *GameNode

		//mObj IGameBase
		X int
		Y int
	}
)

var (
	m_XNode *GameNode
	m_YNode *GameNode
)

func main() {
	node1 := &GameNode{
		X: 13,
		Y: 23,
	}
	node2 := &GameNode{
		X: 13,
		Y: 23,
	}
	node3 := &GameNode{
		X: 14,
		Y: 24,
	}
	AddNode(node1)
	AddNode(node2)
	AddNode(node3)
	//LeaveNode(node2)
	//AddNode(node2)
	//fmt.Println(m_XNode, m_YNode)
	AOI(node3, 3, 5)
}

//添加到十字链路
func AddNode(node *GameNode) {
	//x handle
	var tail *GameNode
	bFind := false
	if m_XNode == nil || m_YNode == nil {
		m_XNode, m_YNode = node, node
		return
	}
	for curNode := m_XNode; curNode != nil; curNode = curNode.xNext {
		// insert data
		if curNode.X > node.X {
			node.xNext = curNode
			if curNode.xPrev != nil {
				node.xPrev = curNode.xPrev
				curNode.xPrev.xNext = node
			} else {
				m_XNode = node
			}
			curNode.xPrev = node
			bFind = true
			break
		}
		tail = curNode
	}

	if tail != nil && !bFind {
		tail.xNext = node
		node.xPrev = tail
	}

	tail = nil
	bFind = false
	//y handle
	for curNode := m_YNode; curNode != nil; curNode = curNode.yNext {
		// insert data
		if curNode.Y > node.Y {
			node.yNext = curNode
			if curNode.yPrev != nil {
				node.yPrev = curNode.yPrev
				curNode.yPrev.yNext = node
			} else {
				m_YNode = node
			}
			curNode.yPrev = node
			bFind = true
			break
		}
		tail = curNode
	}

	if tail != nil && !bFind {
		tail.yNext = node
		node.yPrev = tail
	}
}

//删除到十字链路
func LeaveNode(node *GameNode) {
	if node == m_XNode {
		if node.xNext != nil {
			m_XNode = node.xNext
			if m_XNode.xPrev != nil {
				m_XNode.xPrev = nil
			}
		} else {
			m_XNode = nil
		}
	} else if node.xPrev != nil && node.xNext != nil {
		node.xPrev.xNext = node.xNext
		node.xNext.xPrev = node.xPrev
	} else if node.xPrev != nil {
		node.xPrev.xNext = nil
	}

	if node == m_YNode {
		if node.yNext != nil {
			m_YNode = node.yNext
			if m_YNode.yPrev != nil {
				m_YNode.yPrev = nil
			}
		} else {
			m_YNode = nil
		}
	} else if node.yPrev != nil && node.yNext != nil {
		node.yPrev.yNext = node.yNext
		node.yNext.yPrev = node.yPrev
	} else if node.yPrev != nil {
		node.yPrev.yNext = nil
	}

	node.xPrev = nil
	node.xNext = nil
	node.yPrev = nil
	node.yNext = nil
}

//AOI
func AOI(node *GameNode, xLen, yLen float32) {
	// 往后找
	for cur := node.xNext; cur != nil; cur = cur.xNext {
		if math.Abs(float64(cur.X-node.X)) <= float64(xLen) && math.Abs(float64(cur.Y-node.Y)) <= float64(yLen) {
			fmt.Println(cur.X, cur.Y)
		}
		//if float64(cur.X-node.X) > float64(xLen) {
		//	break
		//} else {
		//	if math.Abs(float64(cur.Y-node.Y)) <= float64(yLen) {
		//		fmt.Println(cur.X, cur.Y)
		//	}
		//}
	}

	// 往前找
	for cur := node.xPrev; cur != nil; cur = cur.xPrev {
		if math.Abs(float64(cur.X-node.X)) <= float64(xLen) && math.Abs(float64(cur.Y-node.Y)) <= float64(yLen) {
			fmt.Println(cur.X, cur.Y)
		}
		//if float64(cur.X-node.X) > float64(xLen) {
		//	break
		//} else {
		//	if math.Abs(float64(cur.Y-node.Y)) <= float64(yLen) {
		//		fmt.Println(cur.X, cur.Y)
		//	}
		//}
	}
}
